package com.xbk.seckill.user.interfaces.controller;

import com.xbk.seckill.common.exception.ErrorCode;
import com.xbk.seckill.common.model.dto.SeckillUserDTO;
import com.xbk.seckill.common.response.ResponseMessage;
import com.xbk.seckill.common.response.ResponseMessageBuilder;
import com.xbk.seckill.ratelimiter.concurrent.annotation.ConcurrentRateLimiter;
import com.xbk.seckill.ratelimiter.qps.annotation.SeckillRateLimiter;
import com.xbk.seckill.user.application.service.SeckillUserService;
import com.xbk.seckill.user.domain.model.entity.SeckillUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户相关的接口
 *
 * @author xiexu
 * @date 2025/1/11 08:06
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SeckillUserController {

    @Autowired
    private SeckillUserService seckillUserService;

    /**
     * 登录系统
     */
    @RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<String> login(@RequestBody SeckillUserDTO seckillUserDTO) {
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillUserService.login(seckillUserDTO.getUserName(), seckillUserDTO.getPassword()));
    }

    /**
     * 获取用户信息
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
//    @SeckillRateLimiter(permitsPerSecond = 1, timeout = 0)
//    @ConcurrentRateLimiter(name = "bhRateLimiter", queueCapacity = 0)
    public ResponseMessage<SeckillUser> get(@RequestParam String username) {
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillUserService.getSeckillUserByUserName(username));
    }

    @RequestMapping(value = "/sleuth/filter/api", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<String> sleuthFilter(HttpServletRequest request) {
        Object traceIdObj = request.getAttribute("traceId");
        String traceId = traceIdObj == null ? "" : traceIdObj.toString();
        log.info("获取到的traceId为: " + traceId);
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), "sleuthFilter");
    }

    @RequestMapping(value = "/async/api", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<String> asyncApi() {
        log.info("执行异步任务开始...");
        seckillUserService.asyncMethod();
        log.info("异步任务执行结束...");
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), "asyncApi");
    }

    @GetMapping(value = "/api1/demo1")
    public ResponseMessage<String> api1Demo1() {
        log.info("访问了api1Demo1接口");
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), "api1Demo1");
    }

    @GetMapping(value = "/api1/demo2")
    public ResponseMessage<String> api1Demo2() {
        log.info("访问了api1Demo2接口");
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), "api1Demo2");
    }

    @GetMapping(value = "/api2/demo1")
    public ResponseMessage<String> api2Demo1() {
        log.info("访问了api2Demo1接口");
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), "api2Demo1");
    }

    @GetMapping(value = "/api2/demo2")
    public ResponseMessage<String> api2Demo2() {
        log.info("访问了api2Demo2接口");
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), "api2Demo2");
    }

}
